package com.ramos.kuaixiao.network;

import android.os.AsyncTask;

import com.ramos.kuaixiao.App;
import com.ramos.kuaixiao.db.BrandDB;
import com.ramos.kuaixiao.db.EmpBrandDB;
import com.ramos.kuaixiao.db.ProductTypeDB;
import com.ramos.kuaixiao.db.ShopsDB;
import com.ramos.kuaixiao.entity.EmpBrand;
import com.ramos.kuaixiao.entity.ProductType;
import com.ramos.kuaixiao.entity.ShopNew;
import com.ramos.kuaixiao.entity.ShopTypeBean;
import com.ramos.kuaixiao.utils.CommUtil;
import com.ramos.kuaixiao.utils.FileUtil;
import com.ramos.kuaixiao.utils.SharedPreference;
import com.ramos.kuaixiao.utils.StringUtil;
import com.ramos.kuaixiao.utils.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class DownloadEmpBrandTask extends AsyncTask<Void, Integer, Boolean> {
    private final static String TAG = "DownloadEmpBrand";
    String brandList = "";

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
            getAllLocalEmpBrand();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    private void getAllLocalEmpBrand() {
        List<ShopNew> tmpList = new ArrayList<>();
        File file = new File(FileUtil.getDataPath(App.mContext) + "/tbl_productbrand.csv");
        List<ShopNew> list = readBrandCsv(file.getAbsolutePath());

        File file1 = new File(FileUtil.getDataPath(App.mContext) + "/tbl_emp_brand.csv");
        List<EmpBrand> list1 = readEmpBrandCsv(file1.getAbsolutePath());

        EmpBrandDB empBrandDB = new EmpBrandDB();
        empBrandDB.addAllEmpBrand(list1);

        for (EmpBrand empStoreHouse : list1) {
            if (empStoreHouse.getEmpId().equals(SharedPreference.getPreferString("id"))) {
                brandList = empStoreHouse.getBrandList();
            }
        }

        if (!StringUtil.isEmpty(brandList)) {
            String[] list2 = brandList.split("_");
            if (list2.length > 0) {
                for (String s : list2) {
                    for (ShopNew wareHouse : list) {
                        if (s.equals(wareHouse.getBrandId())) {
                            tmpList.add(wareHouse);
                        }
                    }
                }
            }
        } else {
            tmpList.addAll(list);
        }
        CommUtil.d(App.mContext, TAG, "tbl_productbrand:" + list.size());

        File file3 = new File(FileUtil.getDataPath(App.mContext) + "/tbl_producttype.csv");
        List<ProductType> list3 = readProductTypeCsv(file3.getAbsolutePath());
        CommUtil.d(App.mContext, TAG, "tbl_producttype:" + list3.size());
        ProductTypeDB customerDB = new ProductTypeDB();
        customerDB.addAllProductType(list3);

        File file2 = new File(FileUtil.getDataPath(App.mContext) + "/tbl_productinfo.csv");
        List<ShopTypeBean.ShopType.ProductInfo> list2 = readProductInfoCsv(file2.getAbsolutePath());

        List<ShopNew> tmpList1 = new ArrayList<>();
        Set<String> tmpList2 = new HashSet<>();
        for (ShopTypeBean.ShopType.ProductInfo productInfo : list2) {
            tmpList2.add(productInfo.getBrandId());
        }
        CommUtil.d(App.mContext, TAG, "BrandDB:" + tmpList2.size());
        for (String s : tmpList2) {
            for (ShopTypeBean.ShopType.ProductInfo productInfo : list2) {
                if (s.equals(productInfo.getBrandId())) {
                    ShopNew shopNew = new ShopNew();
                    shopNew.setBrandId(productInfo.getBrandId());
                    shopNew.setBrandName(productInfo.getBrandName());
                    tmpList1.add(shopNew);
                    break;
                }
            }
        }
        CommUtil.d(App.mContext, TAG, "BrandDB:" + tmpList1.size());
        BrandDB brandDB = new BrandDB();
        brandDB.addAllBrand(tmpList1);
        CommUtil.d(App.mContext, TAG, "tbl_productinfo:" + list2.size());

        ShopsDB shopsDB = new ShopsDB();
        shopsDB.addAllShops(list2);
        App.mProductInfos.clear();
        App.mProductInfos = new ShopsDB().findAllUseShops();

       /* try {
            ShopTypeBean shopTypeBean = new Test().getProductByBrand();
            List<ShopTypeBean.ShopType> list4 = shopTypeBean.getBrandList();
            Log.d(TAG, "shopTypeBean:" + list4.size());
            if(list4 != null && list4.size() > 0){
                for(ShopTypeBean.ShopType shopType : list4){
                    for(ShopNew shopNew : tmpList){
                        if (shopType.getBrandId().equals(shopNew.getBrandId())){
                            if(shopType.getTypeGroupList() != null && shopType.getTypeGroupList().size() > 0) {
                                shopNew.setBrandProducts(JSONArray.toJSONString(shopType.getTypeGroupList()));
                                Log.d(TAG, "ressetBrandProducts:" + shopType.getBrandId() + " " + shopType.getBrandName() + " " + shopType.getTypeGroupList().size());
                                tmpList1.add(shopNew);
                            }
                        }
                        //break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d(TAG, "tmpList1:" + tmpList1.size());
        BrandDB brandDB = new BrandDB();
        brandDB.addAllBrand(tmpList1);*/
    }

    public List<ShopNew> readBrandCsv(String path) {
        List<ShopNew> readerArr = new ArrayList<>();
        Scanner in = Utils.readCsv(path);
        try {
            in.nextLine();
            while (in.hasNextLine()) {
                String s = in.nextLine();
                String[] lines = s.split(",");
                if (Utils.replaceStr(lines[2]).equals(SharedPreference.getPreferString("dealer"))) {
                    //Log.d(TAG, "Brand:" +  lines[5]);
                    String id = "";
                    String[] ids = Utils.replaceStr(lines[1]).split("_");
                    if (ids.length > 0) {
                        id = ids[1];
                    } else {
                        id = Utils.replaceStr(lines[1]);
                    }
                    ShopNew bean = new ShopNew(id, Utils.replaceStr(lines[4]));
                    readerArr.add(bean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return readerArr;
    }

    private List<EmpBrand> readEmpBrandCsv(String absolutePath) {
        List<EmpBrand> readerArr = new ArrayList<>();
        Scanner in = Utils.readCsv(absolutePath);
        try {
            in.nextLine();
            while (in.hasNextLine()) {
                String s = in.nextLine();
                String[] lines = s.split(",");
                EmpBrand bean = new EmpBrand(Utils.replaceStr(lines[1]), Utils.replaceStr(lines[2]), Utils.replaceStr(lines[3]), Long.valueOf(Utils.replaceStr(lines[4])));
                readerArr.add(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return readerArr;

    }

    public List<ProductType> readProductTypeCsv(String path) {
        List<ProductType> readerArr = new ArrayList<>();
        Scanner in = Utils.readCsv(path);
        try {
            in.nextLine();
            //Log.d(TAG, "ProductType0:" + in.nextLine() );
            while (in.hasNextLine()) {
                String s = in.nextLine();
                String[] lines = s.split(",");
                //Log.d(TAG, "ProductType:" + lines[1] + " " + lines[2] + " " + lines[3] + " " + lines[4] + " " + lines[5]  + " " + lines[8] + " " + System.currentTimeMillis());
                if (Utils.replaceStr(lines[5]).equals(SharedPreference.getPreferString("dealer"))) {
                    // Log.d(TAG, "ProductType1:" + lines[1] + " " + lines[2] + " " + lines[3] + " " + lines[4] + " " + lines[5]  + " " + lines[8]);
                    ProductType bean = new ProductType(Utils.replaceStr(lines[1]), Utils.replaceStr(lines[2]), Utils.replaceStr(lines[3]), Utils.replaceStr(lines[4]), Utils.replaceStr(lines[5]), Integer.valueOf(Utils.replaceStr(lines[8])), System.currentTimeMillis());
                    readerArr.add(bean);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return readerArr;
    }

    public List<ShopTypeBean.ShopType.ProductInfo> readProductInfoCsv(String path) {
        List<ShopTypeBean.ShopType.ProductInfo> readerArr = new ArrayList<>();
        Scanner in = Utils.readCsv(path);
        try {
            in.nextLine();
            while (in.hasNextLine()) {
                String[] lines = in.nextLine().split(",");
                //Log.d(TAG, "ProductInfo:" + lines[8] + " " + lines[9] + " " + lines[4] + " " + lines[5] );
                if (!lines[22].contains("-")) {
                    String[] infos = Utils.replaceStr(lines[1]).split("_");
                    int length = infos.length;
                    if (Utils.replaceStr(lines[14]).equals("CHEC")) {
                        ShopTypeBean.ShopType.ProductInfo bean = new ShopTypeBean.ShopType.ProductInfo(infos[length - 1], Utils.replaceStr(lines[3]), Utils.replaceStr(lines[12]),
                                Integer.valueOf((Utils.replaceStr(lines[22]))), Utils.replaceStr(lines[17]), Utils.replaceStr(lines[14]),
                                Float.valueOf(Utils.replaceStr(lines[15])), Utils.replaceStr(lines[8]), Utils.replaceStr(lines[9]), Utils.replaceStr(lines[4]), Utils.replaceStr(lines[5]), Long.valueOf(Utils.replaceStr(lines[28])));
                        readerArr.add(bean);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return readerArr;
    }

}
